package com.bdqn.ch07.controller;


import com.bdqn.ch07.entity.Role;
import com.bdqn.ch07.entity.User;
import com.bdqn.ch07.service.RoleService;
import com.bdqn.ch07.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    @RequestMapping(value = "/list")
    public String list(Model model) {
      /*  List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setUsrId(1L);
        user1.setUsrName("aa");
        User user2 = new User();
        user2.setUsrId(2L);
        user2.setUsrName("bb");
        list.add(user1);
        list.add(user2);
        model.addAttribute("list", list);*/
        List<User> list = userService.findByUserList();
        model.addAttribute("list", list);
        return "user/list";
    }

    @RequestMapping(value = "/userListPage")
    public String userListPage(Model model,
                               String usrName,  //按照用户名查询
                               @RequestParam(required = false, defaultValue = "0") Long roleId, //按照角色id查询
                               @RequestParam(required = false, defaultValue = "1") int pageIndex) { //当前显示的页码
        System.out.println("usrName：" + usrName);
        System.out.println("roleId：" + roleId);
        System.out.println("pageIndex：" + pageIndex);
        // Page<User> userPage = userService.getUserListPageService(usrName, roleId, pageIndex);

        Map param = new HashMap();
        param.put("usrName", usrName);
        param.put("roleId", roleId);
        //int pageIndex = 1;
        int size = 2; //每页显示数据量;
        Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, size, sort);
        Page<User> userPage = userService.findPageByMap(param, pageable);

        //查询所有角色
        List<Role> roleList = roleService.findByRoleList();
        model.addAttribute("roleList", roleList);

        model.addAttribute("userPager", userPage);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        return "user/list";
    }



/*    @ResponseBody
    @RequestMapping(value = "/add")
    public Object add(Model model) {
        //查询所有角色
        List<Role> roleList = roleService.findByRoleList();
        //model.addAttribute("role",roleList);

        return roleList;
    }*/

    @RequestMapping(value = "/add")
    public Object add(Model model) {
        //查询所有角色
        List<Role> roleList = roleService.findByRoleList();
        model.addAttribute("roleList", roleList);
        //request.setAttribute();
        return roleList;
    }

    @RequestMapping(value = "/save")
    public Object save(User user) {
        System.out.println(user.getUsrName());
        System.out.println(user.getUsrPassword());
        System.out.println(user.getRole().getRoleId());
        try {
            userService.saveUserService(user);
            return "redirect:/user/userListPage";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/error";
        }
    }

    @RequestMapping(value = "/delByUserId")
    @ResponseBody
    public Object delByUserId(Long userId) {
        System.out.println("userId:" + userId);
        Map map = new HashMap();
        try {
            userService.delByUserIdService(userId);
            map.put("delResult", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", "false");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/getuserid/{id}")
    public Object getUser(@PathVariable("id") Long usrId) {
        User user = userService.getUserByIdService(usrId);
        System.out.println(user.toString());
        return user;
    }

    @RequestMapping("/view")
    public Object viewById(@RequestParam(value = "userId") Long userId, Model model) {
        User user = userService.viewByIdService(userId);
        //查询所有角色
        List<Role> roleList = roleService.findByRoleList();
        model.addAttribute("roleList", roleList);
        model.addAttribute("user", user);
        return "user/view";
    }

    @RequestMapping("/edit")
    public Object viewById(User user) {
        try {
            userService.saveUserService(user);
            return "redirect:/user/userListPage";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/error";
        }
    }
}
